package be.groept.ace.cursusplanning.persistency;

import be.groept.ace.cursusplanning.controller.AppConfig;


/**
 * @author Gouke Leplat
 */

public class PersisentieSysteem_ObjectFactory {
	
	@SuppressWarnings("unchecked")
	public static IPersistentieController getIPersistentieController(String name){
		
		IPersistentieController ctrlObject = null;
		
		try {
			
			Class<IPersistentieController> ctrlClass = (Class<IPersistentieController>) Class.forName(name);
			ctrlObject = ctrlClass.newInstance();
			return ctrlObject;
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return ctrlObject;
	}

	@SuppressWarnings("unchecked")
	public static void setPersistentieProviderForObject(AppConfig appConfig, Persisteerbaar obj) {
		
		IPersistentieProvider provider = null;
		
		try {
			Class<IPersistentieProvider> pClass = (Class<IPersistentieProvider>) Class.forName(appConfig.getPersistentieProviderClassName(obj.getClass().getName()));
			provider = pClass.newInstance();
			obj.setPersistentieProvider(provider);
			provider.setTarget(obj);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		
	}
}
